/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package onpu_dms_xls.kernel.common.DB;

import java.io.File;
import java.sql.*;
import onpu_dms_xls.exceptions.MySQLException;
import onpu_dms_xls.kernel.common.logAndDebug.LoggingFacade;
import org.apache.log4j.Logger;

/**
 *
 * @author snbm
 */
public class DBConnect {
    
    
    private Connection conn;

    public DBConnect() {
        this.conn = null;
        String url = "jdbc:postgresql:onpu", user = "postgres", password = "1111";
        try {
            Class.forName("org.postgresql.Driver").newInstance();
        } catch (Exception e) {
            return;
        }
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            return;
        }
        return;
    }

    /**
     *
     */
    public DBConnect(
            String DB_URL,
            String DB_Login,
            String DB_Password)
    throws MySQLException {
        this.conn = null;
        //String url = "jdbc:postgresql:onpu", user = "postgres", password = "1111";
        try {
            Class.forName("org.postgresql.Driver").newInstance();
        } catch (Exception e) {
            throw new MySQLException(
                    "DB",
                    "connectToDB",
                    "error while: Class.forName(\"org.postgresql.Driver\").newInstance();",
                    "n/a",
                    e.getMessage());
        }
        try {
            this.conn = DriverManager.getConnection(DB_URL, DB_Login, DB_Password);
        } catch (SQLException e) {
            throw new MySQLException(
                    "DB",
                    "connectToDB",
                    "error while: conn = DriverManager.getConnection(DB_URL, DB_Login, DB_Password);",
                    "DB_URL= " + DB_URL + " DB_Login= " + DB_Login + "DB_Password= " + DB_Password,
                    e.getMessage());
        }
        LoggingFacade.trace.info("connection to DB successful");
    }

    public java.sql.Connection getConnection(){
        return this.conn;
    }

    public void closeConnection()
            throws MySQLException {
        try {
            this.conn.close();
        } catch (SQLException e) {
            throw new MySQLException(
                    "DB",
                    "closeConnection",
                    "error while: " + "conn.close();",
                    "n/a",
                    e.getMessage());
        } catch (Exception e) {
            LoggingFacade.trace.info("enexpected exception in closeConnection" + e);
            return;
        }
    }
}
